package com.shuo.dsa.niuke;

/**
 * 求指数:
 * 快速幂: 10^1101 = 10^0001*10^0100*10^1000 且 10^0100 * 10^0100 = 10^1000
 */
public class Power {
    public double Power(double base, int exponent) {
        if (exponent == 0) return 1;
        if (exponent == 1) return base;
        double ret = 1;
        double newBase = base;
        int exp = exponent > 0 ? exponent : -exponent;
        while (exp != 0) {
            if ((exp & 1) == 1) {
                ret *= newBase;
            }
            newBase *= base;
            exp >>>= 1;
        }
        return (exponent > 0) ? ret : 1 / ret;
    }
}
